//
//  LYSafeUserInfo.m
//  GeiNiHua
//
//  Created by 吴浪 on 16/6/28.
//  Copyright © 2016年 GNH. All rights reserved.
//

#import "LYSafeUserInfo.h"

#import <objc/runtime.h>

#import "SSKeychain.h"

typedef void (*_VIMP) (id, SEL, ...);
typedef id (*_IMP) (id, SEL, ...);

#define SAFEUSERINFO_KEY        @"" //解密秘钥
#define SAFEUSERINFO_SERVICE    @"com.company.mingyuegucheng.safeuserinfo.service"
#define SAFEUSERINFO_USER_ACCOUNT @"com.company.mingyuegucheng.safeuserinfo.user"


/** 加密后的key **/
#define SAFEUSERINFO_USER_KEY @"" //USER


#pragma mark - keychain配置信息
static NSString *setupSafe(NSString *account, NSString *key) {
    //获取
    NSString *str = [SSKeychain passwordForService:SAFEUSERINFO_SERVICE account:account];
    if (str.length > 0) {
    }else {//存储
#pragma mark - 配置加密解密算法
//        str = [key aes256_decrypt:SAFEUSERINFO_KEY];
        [SSKeychain setPassword:str forService:SAFEUSERINFO_SERVICE account:account];
    }
    return str;
}

#pragma mark - 获取本地用户信息相关交互 秘钥 
static NSString *gainSafeUserInfoKey() {
    return setupSafe(SAFEUSERINFO_USER_ACCOUNT, SAFEUSERINFO_USER_KEY);
}



@interface LYSafeUserInfo ()

@end
@implementation LYSafeUserInfo
static LYSafeUserInfo_t *safe_userInfo = NULL;

+ (LYSafeUserInfo_t *)sharedSafeUserInfo {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        safe_userInfo = malloc(sizeof(LYSafeUserInfo_t));
        safe_userInfo->gainSafeUserInfoKey = gainSafeUserInfoKey;
    });
    return safe_userInfo;
}

+ (void)destroy {
    safe_userInfo ? free(safe_userInfo) : nil;
    safe_userInfo = NULL;
}










#pragma mark - 扩展
+ (void)runVFunction:(NSString *)function
           objcClass:(__unsafe_unretained Class)cls
     isClassFunction:(BOOL)isClsFunction
             operate:(void(^)(_VIMP))operate {
    SEL sel = NSSelectorFromString(function);
    Method method;
    if (isClsFunction) {
        method = class_getClassMethod(cls, sel);
    }else {
        method = class_getInstanceMethod(cls, sel);
    }
    
    if (method && operate) {
        operate((_VIMP)method_getImplementation(method));
    }
}

+ (void)runIFunction:(NSString *)function
           objcClass:(__unsafe_unretained Class)cls
     isClassFunction:(BOOL)isClsFunction
             operate:(void (^)(_IMP imp))operate {
    SEL sel = NSSelectorFromString(function);
    
    Method method;
    if (isClsFunction) {
        method = class_getClassMethod(cls, sel);
    }else {
        method = class_getInstanceMethod(cls, sel);
    }
    
    if (method && operate) {
        operate((_IMP)method_getImplementation(method));
    }
}

@end
